// Xampler, a MIDI-driven sampler
// Copyright (C) 2006 Nick Thomas
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301 USA.

#include "SampleStrip.hpp"
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/messagedialog.h>
#include <gtkmm/stock.h>
using namespace xampler;

bool SampleStrip::m_static_members_initialized = false;
Gtk::FileFilter *SampleStrip::m_filter_sound;
Gtk::FileFilter *SampleStrip::m_filter_any;

SampleStrip::SampleStrip()
{
    m_playing_count = 0;
    set_led_on(false);

    m_sample_name.signal_changed().
        connect(sigc::mem_fun(this, &SampleStrip::on_sample_name_changed));

    m_volume_slider.set_range(0.0, MAX_VOLUME);
    m_volume_slider.set_increments(0.05, 0.2);
    m_volume_slider.set_digits(2);
    m_volume_slider.set_draw_value();
    m_volume_slider.signal_value_changed().
        connect(sigc::mem_fun(this, &SampleStrip::on_volume_slider_changed));

    m_balance_slider.set_range(-1.0, 1.0);
    m_balance_slider.set_increments(0.05, 0.25);
    m_balance_slider.set_digits(2);
    m_balance_slider.set_draw_value();
    m_balance_slider.signal_value_changed().
        connect(sigc::mem_fun(this, &SampleStrip::on_balance_slider_changed));

    m_load_button.set_label("Load");
    m_load_button.signal_clicked().
        connect(sigc::mem_fun(this, &SampleStrip::on_load_button_clicked));

    set_spacing(4);
    set_border_width(3);
    pack_start(m_led, Gtk::PACK_SHRINK);
    pack_start(m_sample_name, Gtk::PACK_EXPAND_WIDGET);
    pack_start(m_volume_slider, Gtk::PACK_EXPAND_WIDGET);
    pack_start(m_balance_slider, Gtk::PACK_EXPAND_WIDGET);
    pack_start(m_load_button, Gtk::PACK_SHRINK);

    if (!m_static_members_initialized) {
        m_filter_sound = new Gtk::FileFilter;
        m_filter_sound->set_name("Sound files");
        m_filter_sound->add_mime_type("audio/wav");
        m_filter_sound->add_mime_type("audio/x-wav");
        m_filter_sound->add_pattern("*.wav");
        m_filter_sound->add_pattern("*.WAV");
        m_filter_sound->add_mime_type("audio/aiff");
        m_filter_sound->add_mime_type("audio/x-aiff");
        m_filter_sound->add_pattern("*.aiff");
        m_filter_sound->add_pattern("*.AIFF");
        m_filter_sound->add_pattern("*.aif");
        m_filter_sound->add_pattern("*.AIF");
        m_filter_sound->add_mime_type("audio/basic");
        m_filter_sound->add_pattern("*.au");
        m_filter_sound->add_pattern("*.AU");
        m_filter_sound->add_pattern("*.snd");
        m_filter_sound->add_pattern("*.SND");
        m_filter_sound->add_pattern("*.paf");
        m_filter_sound->add_pattern("*.PAF");
        m_filter_sound->add_mime_type("application/iff");
        m_filter_sound->add_mime_type("application/x-iff");
        m_filter_sound->add_mime_type("image/iff");
        m_filter_sound->add_mime_type("image/x-iff");
        m_filter_sound->add_pattern("*.svx");
        m_filter_sound->add_pattern("*.SVX");
        m_filter_sound->add_pattern("*.iff");
        m_filter_sound->add_pattern("*.IFF");
        m_filter_sound->add_pattern("*.sf");
        m_filter_sound->add_pattern("*.SF");
        m_filter_sound->add_pattern("*.voc");
        m_filter_sound->add_pattern("*.VOC");
        m_filter_sound->add_pattern("*.w64");
        m_filter_sound->add_pattern("*.W64");
        m_filter_sound->add_pattern("*.pvf");
        m_filter_sound->add_pattern("*.PVF");
        m_filter_sound->add_pattern("*.xi");
        m_filter_sound->add_pattern("*.XI");
        m_filter_sound->add_pattern("*.htk");
        m_filter_sound->add_pattern("*.HTK");
        m_filter_sound->add_pattern("*.caf");
        m_filter_sound->add_pattern("*.CAF");

        m_filter_any = new Gtk::FileFilter;
        m_filter_any->set_name("All files");
        m_filter_any->add_pattern("*");

        m_static_members_initialized = true;
    }
}

void SampleStrip::set_sample(Sample &sample)
{
    m_sample = &sample;
    m_sample_name.set_text(m_sample->get_name());
    m_volume_slider.set_value(m_sample->get_volume());
    m_balance_slider.set_value(m_sample->get_balance());
    set_led_on(false);
}

void SampleStrip::increment_playing_count()
{
    if (m_playing_count == 0) {
        set_led_on(true);
    }

    m_playing_count++;
}

void SampleStrip::decrement_playing_count()
{
    if (m_playing_count > 0) {
        m_playing_count--;

        if (m_playing_count == 0) {
            set_led_on(false);
        }
    }
}

void SampleStrip::set_led_on(bool state)
{
    m_led.set(state ? Gtk::Stock::YES : Gtk::Stock::NO,
              Gtk::ICON_SIZE_MENU);
}

void SampleStrip::on_sample_name_changed()
{
    m_sample->set_name(m_sample_name.get_text());
}

void SampleStrip::on_volume_slider_changed()
{
    m_sample->set_volume(m_volume_slider.get_value());
}

void SampleStrip::on_balance_slider_changed()
{
    m_sample->set_balance(m_balance_slider.get_value());
}

void SampleStrip::on_load_button_clicked()
{
    Gtk::FileChooserDialog file_chooser("Pick a sound file to load",
                                        Gtk::FILE_CHOOSER_ACTION_OPEN);
    file_chooser.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
    file_chooser.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
    file_chooser.add_filter(*m_filter_sound);
    file_chooser.add_filter(*m_filter_any);

    int result = file_chooser.run();

    if (result == Gtk::RESPONSE_OK) {
        Glib::ustring error_message;

        if (!m_sample->load_data(file_chooser.get_filename(),
                                 error_message)) {
            Gtk::MessageDialog error_dialog("Error loading sound file",
                                            false, Gtk::MESSAGE_ERROR);
            error_dialog.set_secondary_text(error_message);
            error_dialog.run();
        }
    } else {
    }
}
